package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.TeacherQuery;
import com.atguigu.guli.service.edu.service.TeacherService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author gyf
 * @since 2020-09-20
 */
@CrossOrigin//解决跨域问题 解除同源策略
@Slf4j //info级别的日志输出
@Api(tags = "讲师管理系统-后端")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController{

    @Autowired
    TeacherService teacherService;

    @ApiOperation("获取所有教师列表")
    @GetMapping("list")
    public R listAll() {
        log.info("获取所有讲师");
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    @ApiOperation("获取所有教师列表(分页)")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam("当前页码") @PathVariable Long page,
                      @ApiParam("每页数量") @PathVariable Long limit
    ) {
        Page<Teacher> teacherPage = new Page<>(page, limit);
        Page<Teacher> pageModel = teacherService.page(teacherPage);
        return R.ok().data("pageModel", pageModel).message("获取当前页讲师列表成功");
    }

    @ApiOperation("获取查询后教师列表(分页)")
    @GetMapping("queryListPage/{page}/{limit}")
    public R queryListPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                           @ApiParam(value = "每页数量", required = true) @PathVariable Long limit,
                           @ApiParam(value = "查询教师条件对象", required = false) TeacherQuery teacherQuery
    ) {
        Page<Teacher> pageModel = new Page<>(page, limit);
        pageModel = teacherService.selectPage(pageModel, teacherQuery);
        return R.ok().data("pageModel", pageModel).message("获取当前页讲师列表成功");
    }

//根据id批量删除
    @ApiOperation(value = "批量删除教师")
    @DeleteMapping("removeList")
    public R removeList(@RequestBody List<String> idList){
        teacherService.removeByIds(idList);
        return R.ok().message("批量删除成功");
    }
    //描述方法和参数
    @ApiOperation(value = "根据id删除教师", notes = "逻辑删除(库中:1是,0否)")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "讲师id", required = true)
                        @PathVariable String id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }


    }


    //添加教师
    @ApiOperation("添加教师")
    @PostMapping("save")
    public R save(@ApiParam(value = "讲师对象", required = true)
                  @RequestBody Teacher teacher
    ) {
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    //根据id修改教师 先查询在修改
    @ApiOperation("根据id查找教师")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "教师id", required = true)
                     @PathVariable String id) {
        Teacher byId = teacherService.getById(id);
        if (byId != null) {
            return R.ok().data("item", byId);
        } else {
            return R.error().message("数据不存在");
        }

    }

    @ApiOperation("更新教师信息")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "教师对象", required = true)
                        @RequestBody Teacher teacher) {
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("更新失败");
        }

    }



}

